Skip to content

adding a util function to calculate decaying reward-rate#65

Merged
hagikent merged 6 commits into
mainfrom
kh-dev_reward-rate
Jun 3, 2025
Merged

adding a util function to calculate decaying reward-rate#65
hagikent merged 6 commits into
mainfrom
kh-dev_reward-rate

Conversation

@hagikent

Copy link
Copy Markdown
Contributor

A function to calculate decaying reward-rate time-series based on discrete reward events.
Exponential or hyperbolic kernel options are available.

  • tau: should be empirically optimised based on RT of mice etc
  • dt: if too small, even fftconvolve gets very slow. For now default ~50ms that corresponds to FIP effective sampling frequency, works with a reasonable computational load.

A function to calculate decaying reward-rate timeseries based on discrete reward events. Exponential or hyperbolic kernel options available.
tau: should be empirically optimised based on RT of mice etc
dt: if too small, even fftconvolve gets very slow. for now default ~50ms that corresponds to FIP effective sampling freqenncy.
@hagikent hagikent requested review from hanhou and rachelstephlee May 31, 2025 23:27
----------
event_times : (N,) array-like
1-D array of reward timestamps [s].
t_start, t_end : float, optional

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would there be a reason to change t_start/t_end to a different time? is there value including this as an input?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the current usage, not really, but if you have multiple sessions in a recording and want to partially calculate RewRate, this is handy. A good option to have as default=None.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep this as it is-- i think we are going to consider multisession analysis in a different capsule. https://github.com/AllenNeuralDynamics/aind-dynamic-foraging-multisession-analysis


Parameters
----------
event_times : (N,) array-like

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be renamed reward_time if this is specific to reward_rate-- if we just want to convolve this to any type of rate, we should rename this function instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. when/if aversive component is integrated, we can do the same excersize to calculate punishment rate, and I was picturing it.
Maybe reward_rate.py, reward_rate() -> event_rate?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would make it event_rate instead. it should be general, so long that the tau, hyper_p are not tuned for reward, but for the trial length

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to your comment above, the latter seems correct.

can you provide documentation for the tau? or can you add a new issue to describe a function to map RT --> tau values? would be nice to add to this PR but we can keep this PR as it is also

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't worked on optimizing tau using RT. I will require a lot of dataset and should be separated from this as it's biology (not a part of util-prep).
will change the name.

@hagikent

hagikent commented Jun 2, 2025

Copy link
Copy Markdown
Contributor Author

will lint soonish

def reward_rate(event_times,
t_start=None,
t_end=None,
dt=1/20, # matching photometry frame rate

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move these comments to the comment block below, so folks can see this when they run 'help' with the function

@rachelstephlee

rachelstephlee commented Jun 2, 2025

Copy link
Copy Markdown
Collaborator

we should move this to metrics folder (or even metrics > trial_metrics.py file) since this is a trial metric and the file already calculates response rate (rolling, not convolved reward rate)

You can keep the inputs, you'll just need to pull reward events from df_trials (should be reward_time_in_session ; reward_outcome_time is usually the end of trial (regardless of whether or not the animal is rewarded)

See:

def compute_trial_metrics(nwb):
    """
    Computes trial by trial metrics

    response_rate,          fraction of trials with a response
    gocue_reward_rate,      fraction of trials with a reward
    response_reward_rate,   fraction of trials with a reward,
                            computed only on trials with a response
    choose_right_rate,      fraction of trials where chose right,
                            computed only on trials with a response
    intertrial_choice,      whether there was an intertrial lick event
    intertrial_choice_rate,   rolling fraction of go cues with intertrial licking
"""

minor filename (reward_rate->event_rate), location (into metrics)
@rachelstephlee

Copy link
Copy Markdown
Collaborator

discussing with @hanhou he agreed the files should be moved in metric, but forgaing_efficiency should be in a different PR with plenty of notice to folks.

from scipy.signal import fftconvolve


def event_rate(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more- to match foraging_efficiency.py and trial_metrics, this function should be called compute_event_rate instead

@rachelstephlee rachelstephlee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good-- i ran it and it matches to what you've shown in our group chats.

i would fix the naming of the function and the final linting (add comment at the start of the file) and we can merge.

@hagikent hagikent requested a review from rachelstephlee June 3, 2025 01:59

@rachelstephlee rachelstephlee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved, thank you!

@hagikent hagikent merged commit f28f8fd into main Jun 3, 2025
3 checks passed
@hagikent hagikent deleted the kh-dev_reward-rate branch September 25, 2025 01:00
@hagikent hagikent restored the kh-dev_reward-rate branch September 25, 2025 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants